package g;

import c.b.a.a.a;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.logging.Logger;
import java.util.zip.CRC32;
import java.util.zip.Deflater;

/* compiled from: GzipSink */
public final class m implements y {
    public final f a;
    public final Deflater b;
    public final i c;
    public boolean d;
    public final CRC32 e = new CRC32();

    public m(y yVar) {
        if (yVar != null) {
            Deflater deflater = new Deflater(-1, true);
            this.b = deflater;
            Logger logger = q.a;
            t tVar = new t(yVar);
            this.a = tVar;
            this.c = new i(tVar, deflater);
            e eVar = tVar.a;
            eVar.F0(8075);
            eVar.B0(8);
            eVar.B0(0);
            eVar.E0(0);
            eVar.B0(0);
            eVar.B0(0);
            return;
        }
        throw new IllegalArgumentException("sink == null");
    }

    public a0 B() {
        return this.a.B();
    }

    public void E(e eVar, long j) throws IOException {
        int i = (j > 0 ? 1 : (j == 0 ? 0 : -1));
        if (i < 0) {
            throw new IllegalArgumentException(a.B("byteCount < 0: ", j));
        } else if (i != 0) {
            v vVar = eVar.a;
            long j2 = j;
            while (j2 > 0) {
                int min = (int) Math.min(j2, (long) (vVar.c - vVar.b));
                this.e.update(vVar.a, vVar.b, min);
                j2 -= (long) min;
                vVar = vVar.f;
            }
            this.c.E(eVar, j);
        }
    }

    public void close() throws IOException {
        if (!this.d) {
            Throwable th = null;
            try {
                i iVar = this.c;
                iVar.b.finish();
                iVar.a(false);
                this.a.P((int) this.e.getValue());
                this.a.P((int) this.b.getBytesRead());
            } catch (Throwable th2) {
                th = th2;
            }
            try {
                this.b.end();
            } catch (Throwable th3) {
                if (th == null) {
                    th = th3;
                }
            }
            try {
                this.a.close();
            } catch (Throwable th32) {
                if (th == null) {
                    th = th32;
                }
            }
            this.d = true;
            if (th != null) {
                Charset charset = b0.a;
                throw th;
            }
        }
    }

    public void flush() throws IOException {
        this.c.flush();
    }
}
